package com.sxz.sbs.module.performance;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.TimePickerView;
import com.sxz.sbs.R;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.base.BasePresenter;
import com.sxz.sbs.module.performance.fragment.FieldRankFragment;
import com.sxz.sbs.module.performance.fragment.MonthRankFragment;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.util.ComponentUtils;
import com.sxz.sbs.widget.CommonTitle;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 业绩排行
 */
@Route(path = RouterUrl.ACHIEVE_RANK)
public class PerformanceRankActivity extends BaseActivity {

    @BindView(R.id.rank_tabLayout)
    TabLayout rankTabLayout;
    @BindView(R.id.rank_viewPager)
    ViewPager rankViewPager;
    @BindView(R.id.tv_rank_date)
    TextView tvRankDate;
    private List<Fragment> fragments = new ArrayList<>(2);
    private String[] titles = new String[]{"本月总榜", "案场排行榜"};
    private TimePickerView timePicker;
    private Date selectDate = new Date();
    private MonthRankFragment monthRankFragment;
    private FieldRankFragment fieldRankFragment;
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("业绩排行榜").build().show();
        tvRankDate.setText(getTime(selectDate));
        monthRankFragment = new MonthRankFragment();
        fieldRankFragment = new FieldRankFragment();
        fragments.add(monthRankFragment);
        fragments.add(fieldRankFragment);
        rankTabLayout.post(new Runnable() {
            @Override
            public void run() {
                ComponentUtils.setIndicator(rankTabLayout, 50, 50);
            }
        });
        rankViewPager.setAdapter(new RankFragmentAdapter(getSupportFragmentManager()));
        rankViewPager.setOffscreenPageLimit(fragments.size()-1);
        rankViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(rankTabLayout));
        rankTabLayout.setupWithViewPager(rankViewPager);
        initPicker();
    }
    /**
     * 时间选择
     */
    private void initPicker() {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(2016, 0, 1);
        Calendar endDate = Calendar.getInstance();
        timePicker = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                selectDate = date;
                tvRankDate.setText(getTime(date));
                notifyUpdate();
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, false, false, false, false})
                .setLabel("年", "月", "", "", "", "")
                .isCenterLabel(false)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(21)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setDecorView(null)
                .build();
    }

    /**
     * 通知更新
     */
    private void notifyUpdate() {
        monthRankFragment.update();
        fieldRankFragment.update();
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM");
        return format.format(date);
    }
    public Date getCurrentDate () {
        return selectDate;
    }
    private class RankFragmentAdapter extends FragmentPagerAdapter {

        public RankFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_performance_rank;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    @OnClick(R.id.rl_rank_date)
    public void onViewClick () {
        if (timePicker != null) {
            timePicker.show();
        }
    }
}
